package pira.elements;

import java.io.PrintStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

import org.neo4j.graphdb.Node;

import pira.db.ui.SubDBUI;

public class Paper extends Neo4jNode {
	
	// except mdate and citeTimes, the others are all String
	public String type;
	public String key;
	private Vector<Author> authors= new Vector<Author>();
	public String editor;
	private String title;
	private String year;
	public String publisher;
	public Date mdate;
	public String bookTitle;
	public String cdrom;
	public String pages;
	public String paperURL;
	public String isbn;
	public int citedTimes= -1;
	public String cite;
	public String series;
	public String ee;
	public String crossref;
	
	public String normalTitle;
	
	private String paperID;
	private String clusterid;
	
	public String getPaperID() {
		return paperID;
	}

	public void setPaperID(String paperID) {
		this.paperID = paperID;
	}


	// constant for the paper node properties
	public static final String TYPE = "type";
	public static final String KEY = "key";
	public static final String AUTHORS = "authors";
	public static final String EDITOR = "editor";
	public static final String TITLE = "title";
	public static final String PUBLISHER = "publisher";
	public static final String MODIFYDATE = "mdate";
	public static final String BOOKTITLE = "book title";
	public static final String CDROM = "cdrom";
	public static final String PAGES = "pages";
	public static final String PAPERURL = "paper URL";
	public static final String ISBN = "isbn";
	public static final String CITEDTIMES = "cited times";
	public static final String CITE = "cite";
	public static final String SERIES = "series";
	public static final String EE = "ee";
	public static final String CROSSREF = "crossref";
	
	
	// constant for the papers index
	public static final String ORDER = "order";
	public static final String PAPERID = "paper id";
	
	public static final String CLUSTERID = "clusterid";
	public static final String YEAR = "year";
	
	public Paper(){}
	
	// constructor for the paper of the small case
	public Paper(String title, String[] names) {
		this.title = title;
		
		this.authors.clear();
		Set<String> uniqueAuthors = new HashSet<String>();
		for (String name : names) {
			if (!uniqueAuthors.contains(name)) {
				uniqueAuthors.add(name);
				this.authors.add(new Author(name));
			}
		}
		
		this.normalTitle = SubDBUI.normal(this.title);
	}
	
	public Paper(Node node) {
		this.type= (String) node.getProperty(TYPE);
		this.key= (String) node.getProperty(KEY);
		
		String[] names= (String[]) node.getProperty(AUTHORS);
		this.authors.clear();
		for (int i= 0; i < names.length; i++)
			this.authors.add(new Author(names[i]));
		
		this.editor= (String) node.getProperty(EDITOR);
		this.title= (String) node.getProperty(TITLE);
		this.publisher= (String) node.getProperty(PUBLISHER);
		
		String date= (String) node.getProperty(MODIFYDATE);
		DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
		try {
			this.mdate= df.parse(date);
		} catch (ParseException e) {
			this.mdate= null;
			e.printStackTrace();
		}
		
		this.bookTitle= (String) node.getProperty(BOOKTITLE);
		this.cdrom= (String) node.getProperty(CDROM);
		this.pages= (String) node.getProperty(PAGES);
		this.paperURL= (String) node.getProperty(PAPERURL);
		this.isbn= (String) node.getProperty(ISBN);
		this.citedTimes=  (Integer) node.getProperty(CITEDTIMES);
		this.cite= (String) node.getProperty(CITE);
		this.series= (String) node.getProperty(SERIES);
		this.ee= (String) node.getProperty(EE);
		this.crossref= (String) node.getProperty(CROSSREF);
		
		this.normalTitle = SubDBUI.normal(this.title);
	}

	// generate paper from old database
	public void setPaperFromOldNode(Node node) {
		this.type= (String) node.getProperty("type");
		this.key= (String) node.getProperty("key");
//		System.out.println(node.getProperty("authors"));
		
		String[] names= (String[]) node.getProperty("authors");
		this.authors.clear();
		for (int i= 0; i < names.length; i++)
			this.authors.add(new Author(names[i]));
		
		this.editor= (String) node.getProperty("editor");
		this.title= (String) node.getProperty("title");
		this.publisher= (String) node.getProperty("publisher");
		
		String date= (String) node.getProperty("publish date");
		DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
		try {
			this.mdate= df.parse(date);
		} catch (ParseException e) {
			this.mdate= null;
			e.printStackTrace();
		}
		
		this.bookTitle= (String) node.getProperty("book title");
		this.cdrom= (String) node.getProperty("cdrom");
		this.pages= (String) node.getProperty("pages");
		this.paperURL= (String) node.getProperty("paper URL");
		this.isbn= (String) node.getProperty("isbn");
		try {
			this.citedTimes= Integer.parseInt((String) node.getProperty("cite times"));
		} catch (ClassCastException e) {
			this.citedTimes = (Integer) node.getProperty("cite times");
		}
		this.cite= (String) node.getProperty("cite");
		this.series= (String) node.getProperty("series");
		this.ee= (String) node.getProperty("ee");
		this.crossref= (String) node.getProperty("crossref");
		
		this.normalTitle = SubDBUI.normal(this.title);
	}

	
	public void print(PrintStream out) {
		out.println("Paper: ");
		out.println("  Type -> " + type); 
		out.println("  KEY -> " + key);
		for (int i= 0; i < authors.size(); i++)
			out.println("  Author -> " + authors.get(i));
		out.println("  Editor -> " + editor);
		out.println("  Title -> " + title);
		out.println("  Publisher -> " + publisher);
		out.println("  Modification Date -> " + mdate);
		out.println("  Book Title ->" + bookTitle);
		out.println("  Cdrom -> " + cdrom);
		out.println("  Pages -> " + pages);
		out.println("  Paper URL -> " + paperURL);
		out.println("  ISBN -> " + isbn);
		out.println("  Cite Times -> " + citedTimes);
		out.println("  Cite -> " + cite);
		out.println("  Series -> " + series);
		out.println("  EE -> " + ee);
		out.println("  Crossref -> " + crossref);
		out.println("  Normal Title -> " + normalTitle);
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
		this.normalTitle = SubDBUI.normal(this.title);
	}

	public Vector<Author> getAuthors() {
		return authors;
	}

	public String getNormalTitle() {
		return this.normalTitle;
	}
	
	public String getYear() {
		return this.year;
	}
	
	public void setYear(String year) {
		this.year = year;
	}
	
	public void setClusterID(String clusterid) {
		this.clusterid = clusterid;
	}
	
	public String getClusterID() {
		return this.clusterid;
	}
}
